import { Toast, Dialog } from "vant";
import cTitle from "components/title";
import goodsCard from "../consumptionComm/goodsCard/goodsCard";
export default {
  components: {
    cTitle,
    goodsCard
  },
  data() {
    return {
      orderId: null,
      type: null,
      orderType: null,
      refundDetails: {},
      orderDetails: [], //订单详情
      receivingStatus: "not_received",
      refundTypes: [],
      reasons: [
        "买多了/买错了",
        "计划有变，暂时不需要了",
        "不能送外卖",
        "使用条件不清楚",
        "联系不上商家",
        "约不到合适的时间",
        "可用的门店距离太远",
        "其他平台/方式购买更优惠",
        "评价不好",
        "担心安全问题（疫情、天气等）"
      ],
      refundValue: "",
      refundRadio: "",
      explain: "",
      refundSum: 1,
      refundShow: false,
      tabIndex: 4
    };
  },
  activated() {
    this.orderId = Number(this.$route.query.orderId);
    this.type = this.$route.query.refundType;
    this.orderType = this.$route.query.orderType;
    this.goodsRefund();
  },
  methods: {
    goodsRefund() {
      let json = {
        order_id: this.orderId,
        type: this.type
      };
      $http.get("refund.apply", json, "正在获取").then(
        response => {
          if (response.result == 1) {
            let res = response.data;
            this.orderDetails = res.order?.order_goods[0];
            this.refundSum = res.order?.order_goods[0]?.total;
          } else {
            this.$dialog.alert({ message: response.msg });
          }
        },
        function(response) {
          // console.log(response);
          // error callback
        }
      );
    },
    refundBtn() {
      if (this.fun.isTextEmpty(this.refundRadio)) {
        Toast("请至少选择一项");
        return;
      }
      this.refundValue = this.refundRadio;
      this.refundShow = false;
    },
    onRefund() {
      if (this.fun.isTextEmpty(this.refundValue)) {
        Toast("请选择退款原因");
        return;
      }
      Dialog.confirm({
        message: "退款申请一旦提交，将不能撤销，是否确认继续退款？"
      })
        .then(() => {
          let json = {
            content: this.explain,
            freight_price: 0,
            order_id: this.orderId,
            other_price: 0,
            part_refund: 1,
            reason: this.refundValue,
            receive_status: 0,
            refund_id: this.orderId,
            refund_type: 0,
            refund_way_type: 0
          };
          $http.post("refund.apply.store", json, "正在获取").then(
            response => {
              if (response.result == 1) {
                Toast(response.msg);
                if (this.orderType) {
                  this.$router.go(-2);
                } else {
                  this.$router.go(-1);
                }
              } else {
                this.$dialog.alert({ message: response.msg });
              }
            },
            function(response) {
              // console.log(response);
              // error callback
            }
          );
        })
        .catch(() => {
          // on cancel
        });
    },
    onClose() {
      this.refundShow = false;
    },
    onrefundChange(event) {
      this.refundRadio = event;
    },
    onCause() {
      this.refundShow = true;
    }
  }
};
